package layeredcontent.views
{
	import flash.desktop.Clipboard;
	import flash.desktop.ClipboardFormats;
	import flash.desktop.NativeDragManager;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.EventPhase;
	import flash.events.NativeDragEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	
	import mx.core.IMXMLObject;

	public class TemplateDragOver implements IMXMLObject
	{
		protected var screen:Sprite;
		protected var overlay:Sprite;
		
		public function TemplateDragOver()
		{
		}

		public function initialized(document:Object, id:String):void
		{
			screen = document as Sprite;
			screen.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
		}
		
		protected function onAddedToStage(event:Event):void
		{
			createOverlay();
			overlay.visible = false;
			screen.stage.addEventListener(NativeDragEvent.NATIVE_DRAG_ENTER, onDragEnter);
		}
		
		protected function onDragEnter(event:NativeDragEvent):void
		{
			if (event.eventPhase != EventPhase.AT_TARGET) {
				return;
			}
			
			var url:String;
			var clipboard:Clipboard = event.clipboard;
			
			if (clipboard.hasFormat(ClipboardFormats.URL_FORMAT)) {
				url = event.clipboard.getData(ClipboardFormats.URL_FORMAT) as String;
			} else if (clipboard.hasFormat(ClipboardFormats.TEXT_FORMAT)) {
				var text:String = clipboard.getData(ClipboardFormats.TEXT_FORMAT) as String;
				if (text.match(/^\w+:\/\/.*/)) {
					url = text;
				}
			}
			
			if (url) {
				trace(url);
				trace(event.dropAction);
				NativeDragManager.acceptDragDrop(screen.stage);
				screen.stage.addEventListener(NativeDragEvent.NATIVE_DRAG_OVER, onDragOver);
				screen.stage.addEventListener(NativeDragEvent.NATIVE_DRAG_EXIT, onDragExit);
				screen.stage.addEventListener(NativeDragEvent.NATIVE_DRAG_DROP, onDragExit);
				updateOverlay();
				overlay.visible = true;
			}
		}
		
		protected function onDragOver(event:NativeDragEvent):void
		{
			NativeDragManager.acceptDragDrop(screen.stage);
		}
		
		
		protected function onDragExit(event:NativeDragEvent):void
		{
			if (event.eventPhase != EventPhase.AT_TARGET) {
				return;
			}
			
			overlay.visible = false;
			screen.stage.removeEventListener(NativeDragEvent.NATIVE_DRAG_EXIT, onDragExit);
			screen.stage.removeEventListener(NativeDragEvent.NATIVE_DRAG_DROP, onDragExit);
		}
		
		
		protected function updateOverlay():void
		{
			overlay.graphics.clear();
			overlay.graphics.beginFill(0x000000, .7);
			overlay.graphics.drawRect(0, 0, screen.width, screen.height);
			var msg:TextField = overlay.getChildAt(0) as TextField;
			msg.x = (screen.width - msg.width)/2;
			msg.y = (screen.height - msg.height)/2;
		}
		
		
		protected function createOverlay():void
		{
			overlay = new Sprite();
			var msg:TextField = new TextField();
			msg.autoSize = TextFieldAutoSize.CENTER;
			msg.text = "Drop to\nImport Template";
			msg.setTextFormat(new TextFormat("_sans", 40, 0xFFFFFF, true, false, false, null, null, "center"));
			overlay.addChild(msg);
			msg.x = (overlay.width - msg.width)/2;
			msg.y = (overlay.height - msg.height)/2;
			screen.stage.addChild(overlay);
		}
	}
}